package cn.neo.support.iv;

import android.graphics.Bitmap;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;

import com.facebook.common.references.CloseableReference;
import com.facebook.imageformat.ImageFormat;
import com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory;
import com.facebook.imagepipeline.request.BasePostprocessor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * <b>Name:</b>自定义BasePostprocessor<br>
 * <b>Author:</b> WK（wangkun@xjzhicheng.com)<br>
 * <b>Date:</b> 2016-12-26<br>
 * <b>Describe:</b>用于处理图片的类，在图片尚未传给process之前处理<br>
 * <b>Version:</b>V1.0<br>
 * <b>Revision History:</b><br>
 */
public class MyBasePostProcessor extends BasePostprocessor {

    Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    ImageFormat PNG = new ImageFormat("", "PNG");
    NeoSimpleDraweeView draweeView;

    public MyBasePostProcessor(NeoSimpleDraweeView draweeView) {
        this.draweeView = draweeView;
    }

    @Override
    public CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) {

        // 默认宽高比例显示 W:H = 1:2
        // 按照宽高比例截取图片区域
        if (sourceBitmap.getHeight() > (int) (sourceBitmap.getWidth() * NeoSimpleDraweeView.DEF_RATIO)) {
            Bitmap bitmap = decodeRegion(sourceBitmap, sourceBitmap.getWidth(), (int) (sourceBitmap.getWidth() * NeoSimpleDraweeView.DEF_RATIO));
            return super.process(bitmap, bitmapFactory);
        }

        // 将PNG图片转换成JPG，并将背景色设置为指定颜色
        else if ("PNG".equals(draweeView.getImageFormat()) && draweeView.isReplacePNGBackground() != -1) {
            replaceTransparent2TargetColor(sourceBitmap, draweeView.isReplacePNGBackground());
        }

        // PNG图片，并且设置了图片最大宽高，如果加载的PNG图片宽高超过指定宽高，并截取指定大小
        else if ("PNG".equals(draweeView.getImageFormat())
                && draweeView.getTargetImageSize() != -1
                && (sourceBitmap.getWidth() > draweeView.getTargetImageSize() || sourceBitmap.getHeight() > draweeView.getTargetImageSize())) {

            // 压缩图片
            Bitmap bitmap = IVUtils.decodeSampledBitmapFromByteArray(
                    bitmap2Bytes(sourceBitmap, 100),
                    draweeView.getTargetImageSize(),
                    draweeView.getTargetImageSize());

            // 截取图片
            Bitmap region = decodeRegion(bitmap, draweeView.getTargetImageSize(), draweeView.getTargetImageSize());
            bitmap.recycle();

            return super.process(region, bitmapFactory);
        }
        return super.process(sourceBitmap, bitmapFactory);
    }

    private void replaceTransparent2TargetColor(Bitmap sourceBitmap, int color) {
        Canvas canvas = new Canvas(sourceBitmap);
        canvas.drawColor(color, PorterDuff.Mode.DST_OVER);
        canvas.drawBitmap(sourceBitmap, 0, 0, mPaint);
    }

    /**
     * 将bitmap转化为数组
     */
    public static byte[] bitmap2Bytes(Bitmap bitmap, int quality) {
        if (bitmap == null) {
            throw new IllegalArgumentException("bitmap is not null");
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
        return baos.toByteArray();
    }

    /**
     * 截取bitmap指定的宽高
     */
    public static Bitmap decodeRegion(Bitmap bitmap, int width, int height) {
        return decodeRegion(bitmap2Bytes(bitmap, 100), width, height);
    }

    public static Bitmap decodeRegion(byte[] bytes, int width, int height) {
        BitmapRegionDecoder bitmapRegionDecoder = null;
        try {
            bitmapRegionDecoder = BitmapRegionDecoder.newInstance(bytes, 0, bytes.length, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Rect rect = new Rect(0, 0, width, height);
        assert bitmapRegionDecoder != null;
        return bitmapRegionDecoder.decodeRegion(rect, null);
    }
}
